package com.pengtu.web.app;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.pengtu.dao.Page;
import com.pengtu.dao.QueryFilter;
import com.pengtu.entity.app.ExternalOrganization;
import com.pengtu.service.app.AccountManager;
import com.pengtu.utils.web.Struts2Utils;
import com.pengtu.web.BaseActionSupport;

/**
 * 外部单位管理
 * 
 * @author libitum
 * 
 */
@Namespace("/account")
@Results({ @Result(name = BaseActionSupport.RELOAD, type = "redirect", location = "external_organization.action") })
public class ExternalOrganizationAction extends
		BaseActionSupport<ExternalOrganization> {

	private static final long serialVersionUID = 1L;

	private AccountManager accountManager;

	// -- 页面属性 --//
	private String id;
	private ExternalOrganization entity;
	private List<ExternalOrganization> allExternalOrganizationList;
	private Page<ExternalOrganization> page = new Page<ExternalOrganization>(10);// 每页5条记录

	public AccountManager getAccountManager() {
		return accountManager;
	}

	public void setAccountManager(AccountManager accountManager) {
		this.accountManager = accountManager;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public ExternalOrganization getEntity() {
		return entity;
	}

	public void setEntity(ExternalOrganization entity) {
		this.entity = entity;
	}

	public List<ExternalOrganization> getAllExternalOrganizationList() {
		return allExternalOrganizationList;
	}

	public Page<ExternalOrganization> getPage() {
		return page;
	}

	public void setPage(Page<ExternalOrganization> page) {
		this.page = page;
	}

	@Override
	public ExternalOrganization getModel() {
		// TODO Auto-generated method stub
		return entity;
	}

	public String list() throws Exception {
		// TODO Auto-generated method stub
		QueryFilter queryfilter = QueryFilter.buildFromHttpRequest(Struts2Utils.getRequest());
		queryfilter.addFilter("type","0","NE");		
		// 设置默认排序方式
		if (!page.isOrderBySetted()) {
			page.setOrderBy("id");
			page.setOrder(Page.DESC);
		}
		page = accountManager.searchExternalOrganization(page, queryfilter);
		return SUCCESS;
	}

	@Action("/account/api/external_organization")
	public String apilist() throws Exception {
		QueryFilter queryfilter = QueryFilter.buildFromHttpRequest(Struts2Utils.getRequest());
		queryfilter.addFilter("type","0","NE");		
		// 设置默认排序方式
		if (!page.isOrderBySetted()) {
			page.setOrderBy("id");
			page.setOrder(Page.DESC);
		}
		page = accountManager.searchExternalOrganization(page, queryfilter);
		return SUCCESS;
	}

	@Override
	public String input() throws Exception {
		// TODO Auto-generated method stub
		allExternalOrganizationList = accountManager.getExternalOrganization();
		return INPUT;
	}

	@Override
	public String save() throws Exception {
		// TODO Auto-generated method stub
		accountManager.saveExternalOrganization(entity);
		addActionMessage("保存单位成功");
		return RELOAD;
	}

	@Override
	public String delete() throws Exception {
		// TODO Auto-generated method stub
		accountManager.deleteExternalOrganization(id);
		return RELOAD;
	}

	@Override
	protected void prepareModel() throws Exception {
		// TODO Auto-generated method stub
		if (StringUtils.isNotEmpty(id)) {
			entity = accountManager.getExternalOrganization(id);
		} else {
			entity = new ExternalOrganization();
		}
	}

	@Action("/account/json/external_organization")
	public String searchForTree() throws Exception {
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		List<ExternalOrganization> organizations = accountManager
				.getExternalOrganization();

		for (ExternalOrganization organization : organizations) {
			List<ExternalOrganization> childrenOrgList1 = organization
					.getChildrenOrgans();
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("expanded", Boolean.TRUE);
			map.put("text", organization.getOrgName());
			map.put("id", organization.getId());
			if (childrenOrgList1.isEmpty()) {
				map.put("leaf", Boolean.TRUE);
			} else {
				List<Map<String, Object>> list1 = new ArrayList<Map<String, Object>>();
				for (ExternalOrganization childrenOrg1 : childrenOrgList1) {
					Map<String, Object> map1 = new HashMap<String, Object>();
					map1.put("text", childrenOrg1.getOrgName());
					map1.put("id", childrenOrg1.getId());
					List<ExternalOrganization> childrenOrgList2 = childrenOrg1
							.getChildrenOrgans();
					if (childrenOrgList2.isEmpty()) {
						map1.put("leaf", Boolean.TRUE);
					} else {
						List<Map<String, Object>> list2 = new ArrayList<Map<String, Object>>();
						for (ExternalOrganization childrenOrg2 : childrenOrgList2) {
							Map<String, Object> map2 = new HashMap<String, Object>();
							map2.put("text", childrenOrg2.getOrgName());
							map2.put("id", childrenOrg2.getId());
							List<ExternalOrganization> childrenOrgList3 = childrenOrg2
									.getChildrenOrgans();
							if (childrenOrgList3.isEmpty()) {
								map2.put("leaf", Boolean.TRUE);
							} else {
								List<Map<String, Object>> list3 = new ArrayList<Map<String, Object>>();
								for (ExternalOrganization childrenOrg3 : childrenOrgList3) {
									Map<String, Object> map3 = new HashMap<String, Object>();
									map3.put("text", childrenOrg3.getOrgName());
									map3.put("id", childrenOrg3.getId());
									map3.put("leaf", Boolean.TRUE);
									list3.add(map3);
								}
								map2.put("children", list3);
							}
							list2.add(map2);
						}
						map1.put("children", list2);
					}
					list1.add(map1);
				}
				map.put("children", list1);
			}
			list.add(map);
		}
		Struts2Utils.renderJson(list, "no-cache");
		return null;
	}

	/**
	 * 支持使用Jquery.validate Ajax检验用户名是否重复.
	 */
	public String checkOrgName() {
		HttpServletRequest request = ServletActionContext.getRequest();
		String newOrgName = request.getParameter("orgName");
		String oldOrgName = request.getParameter("oldOrgName");

		if (accountManager.isOrgNameUnique(newOrgName, oldOrgName)) {
			Struts2Utils.renderText("true");
		} else {
			Struts2Utils.renderText("false");
		}
		// 因为直接输出内容而不经过jsp,因此返回null.
		return null;
	}

}
